﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Interop = Microsoft.Office.Interop;
using Tools = Microsoft.Office.Tools;
using System.ComponentModel;

namespace FarpacHelper
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Helper g_Helper;
        public MainWindow(Helper helper)
        {
            // Init Wrapper before call Initialize. Or can implement INotifyChanged then can Init anywhere.
            listTD_PreviewDropFromDiffWrapper = new DropCommand(listTD_PreviewDropFromDiff);
            listTD_PreviewDropToDiffWrapper = new DropCommand(listTD_PreviewDropToDiff);

            InitializeComponent();
            this.widthCollapsed = this.Width;
            this.g_Helper = helper;

            // Tools
            g_Helper.ToolCollection.ToolstringDiagram.Clear(); // Clear first to avoid any problem & increase Speed also
            this.DataContext = g_Toolstring = g_Helper.ToolCollection;
            // Presets
            this.cbPreset.DisplayMemberPath = "Name";
            this.cbPreset.ItemsSource = g_Helper.PresetCollection;
            this.cbPreset.SelectedIndex = 0;
            // Runs
            this.cbRunNo.ItemsSource = g_Helper.RunCollection;
            //g_tsButton = g_tsSheet.Controls[0] as Microsoft.Office.Tools.Excel.Controls.Button; // Button 1 should use Reflection better
        }
        //============================================================================================================================//
        public MainWindow()
        {
            listTD_PreviewDropFromDiffWrapper = new DropCommand(listTD_PreviewDropFromDiff);
            listTD_PreviewDropToDiffWrapper = new DropCommand(listTD_PreviewDropToDiff);

            InitializeComponent();
            this.DataContext = g_Toolstring = new ToolCollection();
            this.widthCollapsed = this.Width;
        }
        //============================================================================================================================//
        //============================================================================================================================//
        //============================================================================================================================//
        public ICommand listTD_PreviewDropFromDiffWrapper { get; private set; }
        private void listTD_PreviewDropFromDiff(object draggedItem)
        {
            Tool draggedTool = draggedItem as Tool; //this.format.Name
            _tsLengthCM += draggedTool.LengthCM;

            //double od = draggedTool.DiameterCM;
            _tsMaxOD = Math.Max(_tsMaxOD, draggedTool.DiameterCM); //if (_StringMaxOD < od) _StringMaxOD = od;
            _tsMinOD = Math.Min(_tsMinOD, draggedTool.DiameterCM); //if (_StringMinOD > od) _StringMinOD = od;

            /*foreach (Tool tool in this.g_Toolstring.ToolstringDiagram)
            {
                totalLen += tool.LengthCM;
            }*/

            RedrawToolstring();
            //MessageBox.Show(string.Format("Len: {0}\r\nMaxOD: {1}\r\nMinOD: {2}", _tsLengthCM.ToString("0.##"), _tsMaxOD.ToString("0.##"), _tsMinOD.ToString("0.##")));
        }
        //============================================================================================================================//
        public ICommand listTD_PreviewDropToDiffWrapper { get; private set; }
        private void listTD_PreviewDropToDiff(object draggedItem)
        {
            Tool draggedTool = draggedItem as Tool; //this.format.Name
            //
            _tsLengthCM -= draggedTool.LengthCM;

            if (_tsMaxOD == draggedTool.DiameterCM)
            {
                //int index = this.g_Toolstring.ToolstringDiagram.IndexOf(draggedTool);
                //List<Tool> tools = this.g_Toolstring.ToolstringDiagram.ToList();
                //tools.RemoveAt(index); 
                _tsMaxOD = 0;
                foreach (Tool tool in this.g_Toolstring.ToolstringDiagram)
                {   // Now Found Max OD
                    _tsMaxOD = Math.Max(_tsMaxOD, tool.DiameterCM);
                }
            }

            if (_tsMinOD == draggedTool.DiameterCM)
            {
                _tsMinOD = double.MaxValue;
                foreach (Tool tool in this.g_Toolstring.ToolstringDiagram)
                    _tsMinOD = Math.Min(_tsMinOD, tool.DiameterCM); // Now Found Min OD
            }

            RedrawToolstring();
            //MessageBox.Show(string.Format("Len: {0}\r\nMaxOD: {1}\r\nMinOD: {2}", _tsLengthCM.ToString("0.##"), _tsMaxOD.ToString("0.##"), _tsMinOD.ToString("0.##")));
        }
        //============================================================================================================================//
        //============================================================================================================================//
        //============================================================================================================================//
        private void cbPreset_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Preset item = (Preset)e.AddedItems[0];

            // Clear Toolstring Diagram First
            for (int i = 0; i < listTD.Items.Count; i++)
            {
                listTD.SetValue(DragDropHelper.DoRemoveItemProperty, new FarpacHelper.DoRemoveItemArgs(listTD.Items[i]));
                i--;
            }


            // Add Toolstring Diagram Later
            foreach (string pToolName in item.ToolString)
            {
                for (int i = 0; i < wpanelTC.Items.Count; i++)
                {
                    Tool tool = wpanelTC.Items[i] as Tool;
                    if (pToolName == tool.Name)
                    {
                        wpanelTC.SetValue(DragDropHelper.DoDragDropProperty, new FarpacHelper.DoDragDropTargetEventArgs(listTD, tool));
                        //i--; // Copy then no need to decrease i or will create infinite loop (system halt)
                    }
                }
            }
        }
        //============================================================================================================================//
        private void cbRunNo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RunIH item = (RunIH)e.AddedItems[0];

            // Clear Toolstring Diagram First
            for (int i = 0; i < listTD.Items.Count; i++)
            {
                listTD.SetValue(DragDropHelper.DoRemoveItemProperty, new FarpacHelper.DoRemoveItemArgs(listTD.Items[i]));
                i--;
            }

            // Add Toolstring Diagram Later
            foreach (string pToolName in item.ToolString)
            {
                for (int i = 0; i < wpanelTC.Items.Count; i++)
                {
                    Tool tool = wpanelTC.Items[i] as Tool;
                    if (pToolName == tool.Name)
                    {
                        wpanelTC.SetValue(DragDropHelper.DoDragDropProperty, new FarpacHelper.DoDragDropTargetEventArgs(listTD, tool));
                        //i--; // Copy then no need to decrease i or will create infinite loop (system halt)
                    }
                }
            }
        }
        //============================================================================================================================//
        private void listTD_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete && e.KeyboardDevice.Modifiers == ModifierKeys.None)
            {
                for (int i = 0; i < listTD.SelectedItems.Count; i++)
                {
                    listTD.SetValue(DragDropHelper.DoRemoveItemProperty, new FarpacHelper.DoRemoveItemArgs(listTD.SelectedItems[i]));
                    i--;
                }
            }
        }
        //============================================================================================================================//
        private void butSave_Click(object sender, RoutedEventArgs e)
        {
            g_Helper.WriteRun(cbRunNo.SelectedItem as RunIH, listTD.Items);
        }
        //============================================================================================================================//
        private void butEditTool_Click(object sender, RoutedEventArgs e)
        {
            new EditToolWindow((this.DataContext as ToolCollection).ToolsCollection).Show();
        }
        //============================================================================================================================//
        double widthExpander = 410;
        double widthCollapsed;
        private void expander1_Expanded(object sender, RoutedEventArgs e)
        {
            this.Width = widthCollapsed + widthExpander;
        }
        //============================================================================================================================//
        private void expander1_Collapsed(object sender, RoutedEventArgs e)
        {
            this.Width = widthCollapsed;
        }
        //============================================================================================================================//
        //Microsoft.Office.Tools.Excel.Controls.Button g_tsButton;
        private void Window_Closed(object sender, EventArgs e)
        {
            //g_tsButton.Enabled = true;
        }

        private void Main_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbRunNo.SelectedIndex = 0; // Leave here so it will load & Redraw Toolstring after init
        }
    }

}
